package com.mobilepay.moneypig.model.activity.mine;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.loonggg.rvbanner.lib.RecyclerViewBanner;
import com.mobilepay.moneypig.R;
import com.mobilepay.moneypig.base.BaseActivity;
import com.mobilepay.moneypig.config.Key;
import com.mobilepay.moneypig.config.NetConfig;
import com.mobilepay.moneypig.model.activity.home.UpVipActivity;
import com.mobilepay.moneypig.model.application.App;
import com.mobilepay.moneypig.okhttp.HttpUtils;
import com.mobilepay.moneypig.response.ResponseResult;
import com.mobilepay.moneypig.response.mine.VipData;
import com.mobilepay.moneypig.utils.CommonUtil;
import com.mobilepay.moneypig.utils.GsonUtil;
import com.mobilepay.moneypig.utils.acache.ACache;
import com.mobilepay.moneypig.view.DividerLine;
import com.mobilepay.moneypig.view.WebViewActivity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xlm on 2017/7/25 0025
 */

public class VipActivity extends BaseActivity {
    private static String TAG = "VipActivity";
    private RecyclerViewBanner mRecycleBanner;
    private RecyclerView mRecycleView;
    private List<VipData.DataBean.VipBean> vipBeanList;
    private List<VipData.DataBean.ListBean> listBeen;
    private VipData.DataBean dataBean;
    private VipAdapter vipAdapter;
    private SmartRefreshLayout refreshLayout;
    private RelativeLayout mRelativeback;
    private RelativeLayout mVipIntroduce;
    private TextView mTextRate;
    private TextView mTextProfit;

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.activity_mine_vip);
    }

    @Override
    protected void initViews() {
        mRecycleBanner = (RecyclerViewBanner) findViewById(R.id.vip_banner);
        mRecycleView = (RecyclerView) findViewById(R.id.vip_recycle);
        refreshLayout = (SmartRefreshLayout) findViewById(R.id.vip_refresh);
        mRelativeback = (RelativeLayout) findViewById(R.id.vip_back);
        mTextRate = (TextView) findViewById(R.id.collect_rate);
        mVipIntroduce = (RelativeLayout) findViewById(R.id.vip_introduce);
        mTextProfit = (TextView) findViewById(R.id.collect_profit);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        getVipData();
    }

    private void setVipBanner() {
        vipBeanList = new ArrayList<>();
        mRecycleBanner.setRvBannerData(vipBeanList);
        mRecycleBanner.setOnSwitchRvBannerListener(new RecyclerViewBanner.OnSwitchRvBannerListener() {
            @Override
            public void switchBanner(int i, AppCompatImageView appCompatImageView) {
                appCompatImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                Glide.with(appCompatImageView.getContext())
                        .load(vipBeanList.get(i).getPath())
                        .into(appCompatImageView);
            }
        });
        mRecycleBanner.setOnRvBannerClickListener(new RecyclerViewBanner.OnRvBannerClickListener() {
            @Override
            public void onClick(int i) {

            }
        });
    }

    private void setVipAdapter() {
        listBeen = new ArrayList<>();
        vipAdapter = new VipAdapter(getThis());
        vipAdapter.setData(listBeen);
        mRecycleView.setLayoutManager(new LinearLayoutManager(getThis()));
        DividerLine dividerLine = new DividerLine(DividerLine.VERTICAL);
        dividerLine.setSize(1);
        dividerLine.setColor(getResources().getColor(R.color.divider_line));
        mRecycleView.addItemDecoration(dividerLine);
        mRecycleView.setAdapter(vipAdapter);
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {
        setVipBanner();
        setVipAdapter();
        getVipData();
    }

    @Override
    protected void onSetListener() {
        mRelativeback.setOnClickListener(this);
        refreshLayout.setEnableLoadmore(false);
        mTextRate.setOnClickListener(this);
        mTextProfit.setOnClickListener(this);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                getVipData();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.vip_back:
                finish();
                break;
            case R.id.collect_rate:
                Intent intent = new Intent();
                intent.setClass(this, WebViewActivity.class);
                intent.putExtra(Key.TITLE, "费率说明");
                intent.putExtra(Key.URL, NetConfig.COLLECT_RATE);
                startActivity(intent);
                break;
            case R.id.collect_profit:
                Intent intent2 = new Intent();
                intent2.setClass(this, WebViewActivity.class);
                intent2.putExtra(Key.TITLE, "会员收益");
                intent2.putExtra(Key.URL, NetConfig.COLLECT_PROFIT);
                startActivity(intent2);
                break;
        }
    }

    private void getVipData() {
        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", NetConfig.getVip);
        param.add("uid", String.valueOf(App.getApp().getUserInfo().getData().getUid()));
        param.add("image_name", "vip");
        param.add("sign", param.getMd5String());
        HttpUtils.getInstance().params(param).executePost(new HttpUtils.HttpListener() {
            @Override
            public void onException(Throwable e) {
                refreshLayout.finishRefresh();
                if (CommonUtil.isForeground(getThis())) {
                    showToastShort(getString(R.string.net_state));
                    String jsonString = ACache.get(App.getApp()).getAsString(NetConfig.getVip);
                    if (!CommonUtil.isEmpty(jsonString)) {
                        setVipData(jsonString);
                    }
                }
            }

            @Override
            public void onHttpSuccess(String string) {
                super.onHttpSuccess(string);
                refreshLayout.finishRefresh();
                setVipData(string);
            }
        });
    }

    private void setVipData(String string) {
        if (HttpUtils.isJson(string)) {
            ResponseResult responseResult = GsonUtil.GsonToBean(string, ResponseResult.class);
            ACache.get(App.getApp()).put(NetConfig.getVip, string);
            if (responseResult.getStatus() == 0) {
                showToastShort(responseResult.getInfo());
                setTxtVipShow(View.GONE);
            } else {
                VipData data = GsonUtil.GsonToBean(string, VipData.class);
                dataBean = data.getData();
                if (vipBeanList.size() != 0) {
                    vipBeanList.clear();
                }
                if (listBeen.size() != 0) {
                    listBeen.clear();
                }
                vipBeanList.addAll(data.getData().getVip());
                listBeen.addAll(data.getData().getList());
                vipAdapter.setData(listBeen);
                if (listBeen.size() != 0) {
                    setTxtVipShow(View.VISIBLE);
                } else
                    setTxtVipShow(View.GONE);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mRecycleBanner.setRvBannerData(vipBeanList);
                        vipAdapter.notifyDataSetChanged();
                    }
                });
            }
        } else {
            setTxtVipShow(View.GONE);
        }
    }

    private void setTxtVipShow(final int visible) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                findViewById(R.id.vip_introduce).setVisibility(visible);
            }
        });
    }

    private class VipAdapter extends RecyclerView.Adapter<VipViewHolder> {
        private Context context;
        private LayoutInflater inflater;
        private List<VipData.DataBean.ListBean> list;

        public VipAdapter(Context context) {
            this.context = context;
            inflater = LayoutInflater.from(context);
        }

        public void setData(List<VipData.DataBean.ListBean> data) {
            this.list = data;
        }

        @Override
        public VipViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new VipViewHolder(inflater.inflate(R.layout.item_mine_vip, parent, false));
        }

        @Override
        public void onBindViewHolder(VipViewHolder holder, int position) {
            final VipData.DataBean.ListBean bean = list.get(position);
            Glide.with(holder.itemView.getContext())
                    .load(list.get(position).getCover())
                    .placeholder(R.mipmap.icon)
                    .into(holder.mImagCover);
            holder.mTextName.setText(bean.getName());
            String content = bean.getAbstractX().replaceAll("_", "\n");
            holder.mTextAbstract.setText(content);
            if (bean.getId() == 1) {
                if (dataBean.getGrade() == 1) {
                    holder.mImageChoose.setVisibility(View.VISIBLE);
                    holder.mTextUp.setVisibility(View.INVISIBLE);
                } else {
                    holder.mImageChoose.setVisibility(View.INVISIBLE);
                    holder.mTextUp.setVisibility(View.INVISIBLE);
                }
            } else {
                if (dataBean.getGrade() == 1) {
                    holder.mImageChoose.setVisibility(View.INVISIBLE);
                    holder.mTextUp.setVisibility(View.VISIBLE);
                    holder.mTextUp.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent();
                            intent.putExtra("background", bean.getBackground());
                            intent.setClass(getThis(), UpVipActivity.class);
                            startActivity(intent);
                        }
                    });
                } else {
                    holder.mImageChoose.setVisibility(View.VISIBLE);
                    holder.mTextUp.setVisibility(View.INVISIBLE);
                }
            }
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

    }

    private static class VipViewHolder extends RecyclerView.ViewHolder {
        private ImageView mImagCover;
        private TextView mTextName;
        private TextView mTextAbstract;
        private ImageView mImageChoose;
        private TextView mTextUp;

        public VipViewHolder(View itemView) {
            super(itemView);
            mImagCover = (ImageView) itemView.findViewById(R.id.svip_commom);
            mTextName = (TextView) itemView.findViewById(R.id.svip_type);
            mTextAbstract = (TextView) itemView.findViewById(R.id.svip_content);
            mImageChoose = (ImageView) itemView.findViewById(R.id.vip_checkbox);
            mTextUp = (TextView) itemView.findViewById(R.id.svip_pay);
        }
    }
}
